package com.example.timesheet.uitl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class DateUtil {

	public static Date getCurrentWeek() {
		return getWeekDate(Calendar.getInstance()).getTime();
	}

	public static Calendar getWeekDate(Calendar calendar) {
		int day = calendar.get(Calendar.DAY_OF_WEEK);
		if (day <= Calendar.FRIDAY) {
			int temp = Calendar.FRIDAY - (day);
			calendar.add(Calendar.DATE, temp);
			return calendar;
		} else if (day == Calendar.SATURDAY) { // if it is saturday then it
												// should calculate as new week.
			calendar.add(Calendar.DATE, Calendar.FRIDAY);
			return calendar;
		}
		return null;
	}

	public static Date today() {
		return Calendar.getInstance().getTime();

	}

	public static String getDateAsString(Date date) {
		if (date == null) {
			return null;
		}

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return simpleDateFormat.format(date);

	}

	public static boolean isWeekday(Date date) {
		if (date == null) {
			return false;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
			return true;
		} else {
			return false;
		}
	}

	public static Date getDateFromString(String date) throws ParseException {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
		return simpleDateFormat.parse(date);
	}

	public static Date convertDbDateFormat(Date date) throws ParseException {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return simpleDateFormat.getCalendar().getTime();
	}

	public static String getDayAsString(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT,
				Locale.US);
	}

	public static String getDayAsFullString(Date date) {
		if (date == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int day = cal.get(Calendar.DAY_OF_WEEK);
		switch (day) {
		case 1:
			return EDate.SUNDAY.name();
		case 2:
			return EDate.MONDAY.name();
		case 3:
			return EDate.TUESDAY.name();
		case 4:
			return EDate.WEDNESDAY.name();
		case 5:
			return EDate.THURSDAY.name();
		case 6:
			return EDate.FRIDAY.name();
		case 7:
			return EDate.SATURDAY.name();
		
		}

		return null;
	}

	public static Date createDate(int day, int month, int year) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, day);
		cal.set(Calendar.MONTH, (month - 1));
		cal.set(Calendar.YEAR, year);
		return createDate2(day, month, year).getTime();
	}

	public static Calendar createDate2(int day, int month, int year) {
		Calendar cal = Calendar.getInstance();
		cal.set(year, month - 1, day, 0, 0);
		return cal;
	}

	public static Date getBeforeDate(Date date, int howmanyDaysBack) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return null;

	}

	public static Calendar processDateTime(int day, int month, int year) {

		month = month + 1;
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, day);
		cal.set(Calendar.MONTH, (month - 1));
		cal.set(Calendar.YEAR, year);
		return cal;
	}

	public static void main(String[] args) {

		//Calendar calendar = createDate2(21, 12, 2014);
		//System.out.println(getDayAsFullString(calendar.getTime()));
		//System.out.println(getWeekDate(calendar).getTime());

		
		List<Integer> list = new ArrayList<Integer>();
		list.add(0,9);
		list.add(1,9);
		list.add(2,9);
		System.out.println(list);
		
		
	}
}
